<@t.html>
<link rel="stylesheet" href="${baseUrl}public/static/ztree/css/zTreeStyle/zTreeStyle.css">
<div class="container-fluid">
	<div class="row" style="padding:10px;">
		            <button onclick="checkAll();"  class="btn btn-default ">全选</button>
					<button onclick="uncheckAll();"  class="btn btn-default ">取消</button>
					<button onclick="grantRoleAuth();" class="btn btn-default  ">提交</button> 
	</div>
	<div class="row">
		<div class="col-md-12">
			<div class="well well-sm" >
				<div class="zTreeDemoBackground left">
						<ul id="resTree" class="ztree"></ul>
				</div>
			</div>
		</div>
	</div>
</div>
<script type="text/javascript" src="public/static/ztree/js/jquery.ztree.all-3.5.min.js"></script>
<script type="text/javascript">
var setting = {
			callback: {
			},
			check: {
				enable: true,
				chkStyle: "checkbox",
				chkboxType: { "Y": "p", "N": "s" }
			},
			data: {
				key: {
					name: "text",
					url: "xUrl"
				},
				simpleData: {
					enable: true
				}
			}
		};
	var zNodes =${res_tree!''};
	var roleId='${roleId!''}';
	var hasResIds='${hasResIds}';
	
	$(function(){
		   var treeObj =$ .fn.zTree.init($("#resTree"), setting,zNodes);
		   treeObj.expandAll(true);
		   if(hasResIds!=""){
			   var ids_array=hasResIds.split(',');var node;
			   for(var i=0,n=ids_array.length;i<n;i++){
				   node = treeObj.getNodeByParam("id", ids_array[i], null);
				   if(node)treeObj.checkNode(node, true, true);
			   }
		   }
		   
    });
	//设置权限
	function grantRoleAuth(){
		  if(roleId==""){alert('未取到roleID，不能提交!');return;}
		  var treeObj = $ .fn.zTree.getZTreeObj("resTree");
		  var checknodes = treeObj.getCheckedNodes(true);
		  var ids = [];
			if (checknodes && checknodes.length > 0) {
				for ( var i = 0; i < checknodes.length; i++) {
					ids.push(checknodes[i].id);
				}
			}
			$ .axsf('admin/role/grant','rids='+ids.toString()+'&roleId='+roleId,function(d){
    			if(d){
    				        simAlert(d.message);
                }
			});
	}
	
	function checkAll() {
		var treeObj = $ .fn.zTree.getZTreeObj("resTree");
		treeObj.checkAllNodes(true);
	}
	function uncheckAll() {
		var treeObj = $ .fn.zTree.getZTreeObj("resTree");
		treeObj.checkAllNodes(false);
	}
	function checkInverse() {
		var treeObj = $ .fn.zTree.getZTreeObj("resTree");
		var check_nodes = treeObj.getCheckedNodes(true);
		var uncheck_nodes = treeObj.getCheckedNodes(false);
		
		if (uncheck_nodes && uncheck_nodes.length > 0) {
			for ( var i = 0; i < uncheck_nodes.length; i++) {
				treeObj.checkNode(uncheck_nodes[i], true, false);
			}
		}
		if (check_nodes && check_nodes.length > 0) {
			for ( var i = 0; i < checknodes.length; i++) {
				treeObj.checkNode(checknodes[i], false, false);
			}
		}
	}
	
</script>
</@t.html>